﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Command.HelpClass;
using Guestbook.IDal;
using Moon.Orm;
using System.Data.Sql;

namespace IDAL
{
    /// <summary>
    /// 基本服务 提供增、删、改、按ID查 等通用功能
    /// </summary>
    /// <typeparam name="T">实体类</typeparam>
    public class BaseDao<T> : IBaseDao<T> where T : EntityBase, new()
    {
        public StringBuilder GetRemoveParametersSQL(WhereExpression expression)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetRemoveParametersSQL(expression);
            }
        }

        public StringBuilder GetUpdateSQL(EntityBase entity)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetUpdateSQL(entity);
            }
        }

        public StringBuilder GetAddSQL(EntityBase entity)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetAddSQL(entity);
            }
        }

        public object Add(EntityBase entity)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.Add(entity);
            }
        }

        public int Update(EntityBase entity)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.Update(entity);
            }
        }

        public int Remove(WhereExpression expression)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.Remove(expression);
            }
        }

        public long GetCount(WhereExpression expression)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetCount(expression);
            }
        }

        public long GetInt32Count(WhereExpression expression)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetInt32Count(expression);
            }
        }

        public StringBuilder GetCountSQL(WhereExpression expression)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                throw new NotImplementedException();
            }
        }

        public int Remove<T2>() where T2 : MQLBase
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.Remove<T2>();
            }
        }


        public List<T> GetEntities(MQLBase mql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetEntities<T>(mql);
            }
        }

        public List<TG> GetOwnList<TG>(MQLBase mql) where TG : new()
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetOwnList<TG>(mql);
            }
        }

        public List<T> GetOwnList(MQLBase mql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;
              
                return db.GetOwnList<T>(mql);
            }
        }

        public T GetEntity(MQLBase mql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetEntity<T>(mql);
            }
        }

        public object ExecuteScalar(MQLBase mql)
        {
            throw new NotImplementedException();
        }

        public MObject ExecuteScalarToMObject(MQLBase mql)
        {
            throw new NotImplementedException();
        }

        public DataSet ExecuteProToDataSet(string procName, params DbParameter[] parameters)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteProToDataSet(procName, parameters);
            }
        }

        public int ExecuteProWithNonQuery(string procName, params DbParameter[] parameters)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteProWithNonQuery(procName, parameters);
            }
        }

        public List<T2> ExecuteProToOwnList<T2>(string procName, params DbParameter[] parameters) where T2 : new()
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteProToOwnList<T2>(procName, parameters);
            }
        }

     

        public DataSet ExecuteSqlToDataSet(string sql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlToDataSet(sql);
            }
        }

        public DataSet ExecuteSqlToDataSet(string sql, params object[] values)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlToDataSet(sql, values);
            }
        }

        public int ExecuteSqlWithNonQuery(string sql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlWithNonQuery(sql);
            }
        }

        public List<T2> ExecuteSqlToOwnList<T2>(string sql) where T2 : new()
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlToOwnList<T2>(sql);
            }
        }

        public List<T2> ExecuteSqlToOwnList<T2>(string sql, params object[] values) where T2 : new()
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlToOwnList<T2>(sql, values);
            }
        }

     

    

        public int ExecuteSqlWithNonQuery(string sql, params object[] values)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlWithNonQuery(sql, values);
            }
        }

        public List<Dictionary<string, MObject>> ExecuteSqlToDictionaryList(string sql, params object[] values)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlToDictionaryList(sql, values);
            }
        }

        public List<Dictionary<string, MObject>> ExecuteSqlToDictionaryList(string sql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.ExecuteSqlToDictionaryList(sql);
            }
        }

        public List<Dictionary<string, MObject>> GetDictionaryList(MQLBase mql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;

                return db.GetDictionaryList(mql);
            }
        }

        public List<T2> GetPagingData<T2>(PageClass pc) where T2 : new()
        {
            DbParameter[] parameters = {
			           new SqlParameter("@tableName", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@fieldNames", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@pageSize", SqlDbType.Int,4) ,            
                        new SqlParameter("@page", SqlDbType.Int,4) ,            
                     new SqlParameter("@pageCount", SqlDbType.Int,4 ) ,            
                        new SqlParameter("@counts", SqlDbType.Int,4) ,            
                        new SqlParameter("@fieldSort", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@sort", SqlDbType.Bit,1) ,            
                        new SqlParameter("@condition", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@keyID", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@distinct", SqlDbType.Bit,1)                
              
            };



            parameters[0].Value = pc.sys_Table;
            parameters[1].Value = pc.sys_Fields;
            parameters[2].Value = pc.sys_PageSize;
            parameters[3].Value = pc.sys_PageIndex;
            parameters[4].Direction = ParameterDirection.Output;

            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = null;
            parameters[7].Value = 1;
            parameters[8].Value = pc.sys_Where;
            parameters[9].Value = pc.sys_Key;
            parameters[10].Value = 0;

           
            using (var db = Db.CreateDefaultDb())
            {
                //  db.DebugEnabled = true;
               
              List<T2>  list=  db.ExecuteProToOwnList<T2>("proc_DataPagingList", parameters);
              pc.RCount = Convert.ToInt32(parameters[5].Value);
              pc.PCount = Convert.ToInt32(parameters[4].Value);
                return list;
            }
        }
        public int GetCountSQL(string sqltable, string sqlwhere)
        {
            using (var db = Db.CreateDefaultDb())
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat("select count(*) from {0}", sqltable);
                if (!string.IsNullOrEmpty(sqlwhere))
                {
                    sql.AppendFormat(" where {0}",sqlwhere);
                }
              //  string sql = "select count(*) from ControlButtons";
                //  db.DebugEnabled = true;
                DataSet ds = db.ExecuteSqlToDataSet(sql.ToString());
                int count = 0;
                int.TryParse(ds.Tables[0].Rows[0][0].ToString(),out count);
                return count;

            }
        }

        public DbDataReader GetDbDataReader(string commandText, CommandType type)
        {
            using (var db = Db.CreateDefaultDb())
            {
             return   db.GetDbDataReader(commandText, type);
            }
        }

        public object GetDynamicList(string sql, string classname)
        {
            using (var db = Db.CreateDefaultDb())
            {
                return db.GetDynamicList(sql, classname);
          
            }
        }

        public object GetScalar(MQLBase mql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                return db.GetScalar(  mql);
            }
        }

        public MObject GetScalarToMObject(MQLBase mql)
        {
            using (var db = Db.CreateDefaultDb())
            {
                return db.GetScalarToMObject(mql);
            }
        }


        public object GetModelBySql(string sql, string classname)
        {
            using (var db = Db.CreateDefaultDb())
            {
                return db.GetModelBySql(sql, classname);
            }
        }

      


    }

   
}
